﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Aga.GestionDeArchivos.Web.Models.Account;
using Aga.GestionDeArchivos.Services;
using Aga.GestionDeArchivos.Domain.Entities;
using Aga.GestionDeArchivos.Web.Models;

namespace Aga.GestionDeArchivos.Web.Controllers
{
    public class AccountController : ControllerBase
    {

        public IFormsAuthenticationService FormsService { get; set; }
        public UserService UserService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            if (UserService == null) { UserService = new UserService(); }

            base.Initialize(requestContext);
        }

        public ActionResult Index()
        {
            return RedirectToAction("LogOn");
        }

        public ActionResult LogOn()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    User user = UserService.ValidateUser(model.UserName, model.Password);

                    if (user != null)
                    {
                        FormsService.SignIn(model.UserName, model.RememberMe);

                        Session[SessionKey.LoggedInUser] = user;

                        if (Url.IsLocalUrl(returnUrl))
                        {
                            return Redirect(returnUrl);
                        }
                        else
                        {
                            return RedirectToAction("Index", "Home");
                        }
                    }
                }
                catch (ArgumentException ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        public ActionResult LogOff()
        {
            FormsService.SignOut();

            return RedirectToAction("Index", "Home");
        }

        [Authorize]
        public ActionResult ChangePassword()
        {
            ViewBag.PasswordLength = UserService.MinPasswordLength;

            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult ChangePassword(ChangePasswordModel model)
        {
            if (ModelState.IsValid)
            {
                if (UserService.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword, model.ConfirmPassword))
                {
                    return RedirectToAction("ChangePasswordSuccess");
                }
                else
                {
                    ModelState.AddModelError("", "La contraseña actual es incorrecta o la nueva es inválida.");
                }
            }

            // If we got this far, something failed, redisplay form
            ViewBag.PasswordLength = UserService.MinPasswordLength;

            return View(model);
        }

        public ActionResult ChangePasswordSuccess()
        {
            return View();
        }

    }
}
